package com.fw.service.masterdata.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;

import com.fw.service.bean.EntryBean;
import com.fw.service.masterdata.MasterDataService;
import com.fw.service.persistence.BOUserPersistenceService;
import com.fw.service.persistence.MerchantProfilePersistenceService;
import com.fw.service.persistence.MerchantUserPersistenceService;
import com.fw.service.persistence.ServiceProfilePersistenceService;
import com.fw.service.persistence.dao.MerchantProfileDAO;
import com.fw.service.persistence.dao.MerchantUserDAO;
import com.fw.service.persistence.dao.ParentMerchantDAO;
import com.fw.service.persistence.dao.RoleDAO;
import com.fw.service.persistence.dao.RoleMerchantDAO;
import com.fw.service.persistence.dao.ServiceProfileDAO;
import com.fw.service.persistence.dto.BOUser;
import com.fw.service.persistence.dto.MerchantProfile;
import com.fw.service.persistence.dto.MerchantUser;
import com.fw.service.persistence.dto.ParentMerchant;
import com.fw.service.persistence.dto.Role;
import com.fw.service.persistence.dto.RoleMerchant;
import com.fw.service.persistence.dto.ServiceProfile;

public class MasterDataServiceImpl implements MasterDataService {

    @Autowired
    private BOUserPersistenceService boUserPersistenceService;
    @Autowired
    private MerchantProfilePersistenceService merchantProfilePersistenceService;
    @Autowired
    private MerchantUserPersistenceService merchantUserPersistenceService;
    @Autowired
    private ServiceProfilePersistenceService serviceProfilePersistenceService;
    @Autowired
    private ParentMerchantDAO parentMerchantDAO;

    @Autowired
    private MerchantUserDAO merchantUserDAO;

    @Autowired
    private MerchantProfileDAO merchantProfileDAO;

    @Autowired
    private ServiceProfileDAO serviceProfileDAO;

    @Autowired
    private RoleDAO roleDAO;

    @Autowired
    private RoleMerchantDAO roleMerchantDAO;

    @Autowired
    private Properties masterDataProperties;

    private List<EntryBean<String, String>> sortListStringByValue(List<EntryBean<String, String>> entryBeans) {
	List<EntryBean<String, String>> list = new ArrayList<EntryBean<String, String>>();
	if (entryBeans != null && entryBeans.size() > 0) {
	    List<String> strings = new ArrayList<String>();
	    Map<String, EntryBean<String, String>> map = new HashMap<String, EntryBean<String, String>>();
	    for (EntryBean<String, String> entryBean : entryBeans) {
		String key = entryBean.getValue() + entryBean.getKey();
		map.put(key, entryBean);
		strings.add(key);
	    }
	    Collections.sort(strings);
	    for (String str : strings) {
		EntryBean<String, String> entryBean = map.get(str);
		list.add(entryBean);
	    }
	}
	return list;
    }

    private List<EntryBean<Long, String>> sortListLongByValue(List<EntryBean<Long, String>> entryBeans) {
	List<EntryBean<Long, String>> list = new ArrayList<EntryBean<Long, String>>();
	if (entryBeans != null && entryBeans.size() > 0) {
	    List<String> strings = new ArrayList<String>();
	    Map<String, EntryBean<Long, String>> map = new HashMap<String, EntryBean<Long, String>>();
	    for (EntryBean<Long, String> entryBean : entryBeans) {
		String key = entryBean.getValue() + entryBean.getKey();
		map.put(key, entryBean);
		strings.add(key);
	    }
	    Collections.sort(strings);
	    for (String str : strings) {
		EntryBean<Long, String> entryBean = map.get(str);
		list.add(entryBean);
	    }
	}
	return list;
    }

    private List<EntryBean<String, String>> selectPropertyByPrefixKey(String prefixKey) {
	List<EntryBean<String, String>> entryBeans = null;
	if (masterDataProperties != null && masterDataProperties.size() > 0) {
	    entryBeans = new ArrayList<EntryBean<String, String>>();
	    for (Entry<Object, Object> entry : masterDataProperties.entrySet()) {
		if (entry.getKey().toString().startsWith(prefixKey)) {
		    String key = entry.getKey().toString().replaceAll(prefixKey + ".", "").trim();
		    String value = entry.getValue().toString().trim();
		    entryBeans.add(new EntryBean<String, String>(key, value));
		}
	    }
	}
	return this.sortListStringByValue(entryBeans);
    }

    private List<EntryBean<String, String>> selectPropertyByPrefixKeyDisplayValue(String prefixKey) {
	List<EntryBean<String, String>> entryBeans = null;
	if (masterDataProperties != null && masterDataProperties.size() > 0) {
	    entryBeans = new ArrayList<EntryBean<String, String>>();
	    for (Entry<Object, Object> entry : masterDataProperties.entrySet()) {
		if (entry.getKey().toString().startsWith(prefixKey)) {
		    String key = entry.getKey().toString().replaceAll(prefixKey + ".", "").trim();
		    String value = entry.getValue().toString().trim();
		    entryBeans.add(new EntryBean<String, String>(value, key + " (" + value + ")"));
		}
	    }
	}
	return this.sortListStringByValue(entryBeans);
    }

    private List<EntryBean<String, String>> selectPropertyByPrefixKeyDisplay(String prefixKey) {
	List<EntryBean<String, String>> entryBeans = null;
	if (masterDataProperties != null && masterDataProperties.size() > 0) {
	    entryBeans = new ArrayList<EntryBean<String, String>>();
	    for (Entry<Object, Object> entry : masterDataProperties.entrySet()) {
		if (entry.getKey().toString().startsWith(prefixKey)) {
		    String key = entry.getKey().toString().replaceAll(prefixKey + ".", "");
		    entryBeans.add(new EntryBean<String, String>(key, key + " (" + entry.getValue().toString() + ")"));
		}
	    }
	}
	return this.sortListStringByValue(entryBeans);
    }

    @Override
    public List<EntryBean<String, String>> selectAllBOUserStatus() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.boUserStatus");
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantUserStatus() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.merchantUserStatus");
    }

    @Override
    public List<EntryBean<String, String>> selectAllServiceProfileStatus() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.serviceProfileStatus");
    }

    @Override
    public List<EntryBean<String, String>> selectAllServiceProfileHost() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.serviceProfileHost");
    }

    @Override
    public List<EntryBean<String, String>> selectAllServiceProfilePaymentType() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.serviceProfilePaymentType");
    }

    @Override
    public List<EntryBean<String, String>> selectAllBank() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.bank");
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<EntryBean<Long, String>> selectAllBOUserUsername() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<BOUser> boUsers = boUserPersistenceService.selectByStatus(null);
	if (boUsers != null && !boUsers.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (BOUser boUser : boUsers) {
		entryBeans.add(new EntryBean(boUser.getId(), boUser.getUsername()));
	    }
	}
	entryBeans = this.sortListLongByValue(entryBeans);
	return entryBeans;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<EntryBean<Long, String>> selectAllBOUserEmployeeName() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<BOUser> boUsers = boUserPersistenceService.selectByStatus(null);
	if (boUsers != null && !boUsers.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (BOUser boUser : boUsers) {
		entryBeans.add(new EntryBean(boUser.getId(), boUser.getEmployeeName() == null ? "" : boUser.getEmployeeName()));
	    }
	}
	entryBeans = this.sortListLongByValue(entryBeans);
	return entryBeans;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<EntryBean<Long, String>> selectAllBOUserEmployeeId() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<BOUser> boUsers = boUserPersistenceService.selectByStatus(null);
	if (boUsers != null && !boUsers.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (BOUser boUser : boUsers) {
		entryBeans.add(new EntryBean(boUser.getId(), boUser.getEmployeeID() == null ? "" : boUser.getEmployeeID()));
	    }
	}
	entryBeans = this.sortListLongByValue(entryBeans);
	return entryBeans;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<EntryBean<Long, String>> selectAllBOUserRole() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<Role> roles = roleDAO.selectByRoleTypeAndRoleStatus(Role.ROLE_TYPE_BACK_OFFICE_USER_ROLE, Role.STATUS_ACTIVE);
	if (roles != null && !roles.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (Role role : roles) {
		entryBeans.add(new EntryBean(role.getId(), role.getRoleName()));
	    }
	}
	entryBeans = this.sortListLongByValue(entryBeans);
	return entryBeans;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<EntryBean<Long, String>> selectAllServiceProfile() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<ServiceProfile> serviceProfiles = serviceProfileDAO.findAll();
	if (serviceProfiles != null && !serviceProfiles.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (ServiceProfile serviceProfile : serviceProfiles) {
		entryBeans.add(new EntryBean(serviceProfile.getId(), serviceProfile.getServiceId()));
	    }
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantProfileStatus() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.merchantProfileStatus");
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantProfileServiceType() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.merchantProfileServiceType");
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantProfileMccCode() {
	return this.selectPropertyByPrefixKeyDisplay("com.fw.service.masterdata.merchantProfileMccCode");
    }

    public List<EntryBean<String, String>> selectAllMerchantProfileId() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> merchantProfiles = merchantProfilePersistenceService.selectAllMerchantProfileId();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    for (Object merchantProfile : merchantProfiles) {
		String merchantProfileStr = (String) merchantProfile;
		if (merchantProfileStr == null || merchantProfileStr.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(merchantProfileStr, merchantProfileStr));
	    }
	}
	entryBeans = this.sortListStringByValue(entryBeans);
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllMerchantProfileCompanyName() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> merchantProfiles = merchantProfilePersistenceService.selectAllCompanyNameEn();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    for (Object merchantProfile : merchantProfiles) {
		String merchantProfileStr = (String) merchantProfile;
		if (merchantProfileStr == null || merchantProfileStr.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(merchantProfileStr, merchantProfileStr));
	    }
	}
	merchantProfiles = merchantProfilePersistenceService.selectAllCompanyNameTh();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    for (Object merchantProfile : merchantProfiles) {
		String merchantProfileStr = (String) merchantProfile;
		if (merchantProfileStr == null || merchantProfileStr.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(merchantProfileStr, merchantProfileStr));
	    }
	}
	entryBeans = this.sortListStringByValue(entryBeans);
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllMerchantProfileURL() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> merchantProfiles = merchantProfilePersistenceService.selectAllURL();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    for (Object merchantProfile : merchantProfiles) {
		String merchantProfileStr = (String) merchantProfile;
		if (merchantProfileStr == null || merchantProfileStr.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(merchantProfileStr, merchantProfileStr));
	    }
	}
	entryBeans = this.sortListStringByValue(entryBeans);
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllMerchantProfileProjectContactPerson() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> merchantProfiles = merchantProfilePersistenceService.selectAllContact1Name();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    for (Object merchantProfile : merchantProfiles) {
		String merchantProfileStr = (String) merchantProfile;
		if (merchantProfileStr == null || merchantProfileStr.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(merchantProfileStr, merchantProfileStr));
	    }
	}
	merchantProfiles = merchantProfilePersistenceService.selectAllContact2Name();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    for (Object merchantProfile : merchantProfiles) {
		String merchantProfileStr = (String) merchantProfile;
		if (merchantProfileStr == null || merchantProfileStr.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(merchantProfileStr, merchantProfileStr));
	    }
	}
	merchantProfiles = merchantProfilePersistenceService.selectAllContact3Name();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    for (Object merchantProfile : merchantProfiles) {
		String merchantProfileStr = (String) merchantProfile;
		if (merchantProfileStr == null || merchantProfileStr.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(merchantProfileStr, merchantProfileStr));
	    }
	}
	merchantProfiles = merchantProfilePersistenceService.selectAllContact4Name();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    for (Object merchantProfile : merchantProfiles) {
		String merchantProfileStr = (String) merchantProfile;
		if (merchantProfileStr == null || merchantProfileStr.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(merchantProfileStr, merchantProfileStr));
	    }
	}
	entryBeans = this.sortListStringByValue(entryBeans);
	return entryBeans;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<EntryBean<Long, String>> selectAllMerchantProfile() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<MerchantProfile> merchantProfiles = merchantProfileDAO.findAll();
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (MerchantProfile merchantProfile : merchantProfiles) {
		entryBeans.add(new EntryBean(merchantProfile.getId(), merchantProfile.getMerchantNameEn()));
	    }
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectAllParentMerchantStatus() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.parentMerchantStatus");
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<EntryBean<Long, String>> selectAllParentMerchant() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<ParentMerchant> parentMerchants = parentMerchantDAO.findAll();
	if (parentMerchants != null && !parentMerchants.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (ParentMerchant parentMerchant : parentMerchants) {
		entryBeans.add(new EntryBean(parentMerchant.getId(), parentMerchant.getParentName()));
	    }
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectAllTitleName() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.titleName");
    }

    @Override
    public List<EntryBean<String, String>> selectAllRoleStatus() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.roleStatus");
    }

    @Override
    public List<EntryBean<String, String>> selectAllRoleType() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.roleType");
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantRoleStatus() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.merchantRoleStatus");
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantRoleType() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.merchantRoleType");
    }

    @Override
    public List<EntryBean<String, String>> selectAllBusinessPartner() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.businessPartner");
    }

    @Override
    public List<EntryBean<String, String>> selectAllReportName() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.reportName");
    }

    @Override
    public List<EntryBean<String, String>> selectAllPaymentPage() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.paymentPage");
    }

    @Override
    public List<EntryBean<String, String>> selectAllLogType() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.logType");
    }

    @Override
    public List<EntryBean<String, String>> selectAllLogActionType() {
	return this.selectPropertyByPrefixKey("com.fw.service.masterdata.logActionType");
    }

    @Override
    public List<EntryBean<String, String>> selectAllBOUserCreateBy() {
	List<EntryBean<String, String>> entryBeans = null;
	List<Object> objects = boUserPersistenceService.selectDistinctCreateBy();
	if (objects != null && !objects.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<String, String>>();
	    for (Object object : objects) {
		String createBy = (String) object;
		entryBeans.add(new EntryBean<String, String>(createBy, createBy));
	    }
	    entryBeans = this.sortListStringByValue(entryBeans);
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectAllBOUserLastUpdateBy() {
	List<EntryBean<String, String>> entryBeans = null;
	List<Object> objects = boUserPersistenceService.selectDistinctLastUpdateBy();
	if (objects != null && !objects.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<String, String>>();
	    for (Object object : objects) {
		String lastUpdateBy = (String) object;
		entryBeans.add(new EntryBean<String, String>(lastUpdateBy, lastUpdateBy));
	    }
	    entryBeans = this.sortListStringByValue(entryBeans);
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectBOUserStatusByCurrentStatus(String status) {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	String prefix = "com.fw.service.masterdata.boUserStatus.";
	String key = null;
	String value = null;
	key = prefix + status;
	value = masterDataProperties.getProperty(key, null);
	entryBeans.add(new EntryBean<String, String>(status, value));
	if (BOUser.STATUS_ACTIVE.equals(status)) {
	    key = prefix + BOUser.STATUS_EXPIRED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(BOUser.STATUS_EXPIRED, value));
	    key = prefix + BOUser.STATUS_LOCKED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(BOUser.STATUS_LOCKED, value));
	    key = prefix + BOUser.STATUS_INACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(BOUser.STATUS_INACTIVE, value));
	} else if (BOUser.STATUS_EXPIRED.equals(status) || BOUser.STATUS_LOCKED.equals(status)) {
	    key = prefix + BOUser.STATUS_ACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(BOUser.STATUS_ACTIVE, value));
	} else if (BOUser.STATUS_INACTIVE.equals(status)) {
	    key = prefix + BOUser.STATUS_ACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(BOUser.STATUS_ACTIVE, value));
	    key = prefix + BOUser.STATUS_CLOSED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(BOUser.STATUS_CLOSED, value));
	} else if (BOUser.STATUS_CLOSED.equals(status)) {
	    // value = masterDataProperties.getProperty(prefix + BOUser.STATUS_INACTIVE, null);
	    // entryBeans.add(new EntryBean<String, String>(value, value));
	}
	entryBeans = this.sortListStringByValue(entryBeans);
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectAllCountry() {
	return this.selectPropertyByPrefixKeyDisplayValue("com.fw.service.masterdata.country");
    }

    @Override
    public List<EntryBean<String, String>> selectMerchantProfileStatusByCurrentStatus(String status) {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	String prefix = "com.fw.service.masterdata.merchantProfileStatus.";
	String key = null;
	String value = null;
	key = prefix + status;
	value = masterDataProperties.getProperty(key, null);
	entryBeans.add(new EntryBean<String, String>(status, value));
	if (MerchantProfile.STATUS_ACTIVE.equals(status)) {
	    key = prefix + MerchantProfile.STATUS_SUSPENED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantProfile.STATUS_SUSPENED, value));
	    key = prefix + MerchantProfile.STATUS_INACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantProfile.STATUS_INACTIVE, value));
	} else if (MerchantProfile.STATUS_SUSPENED.equals(status)) {
	    key = prefix + MerchantProfile.STATUS_ACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantProfile.STATUS_ACTIVE, value));
	    key = prefix + MerchantProfile.STATUS_INACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantProfile.STATUS_INACTIVE, value));
	} else if (MerchantProfile.STATUS_INACTIVE.equals(status)) {
	    key = prefix + MerchantProfile.STATUS_ACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantProfile.STATUS_ACTIVE, value));
	    key = prefix + MerchantProfile.STATUS_SUSPENED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantProfile.STATUS_SUSPENED, value));
	    key = prefix + MerchantProfile.STATUS_CLOSED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantProfile.STATUS_CLOSED, value));
	} else if (MerchantProfile.STATUS_CLOSED.equals(status)) {
	}
	entryBeans = this.sortListStringByValue(entryBeans);
	return entryBeans;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List<EntryBean<Long, String>> selectAllMerchantUserRole() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<RoleMerchant> roleMerchants = roleMerchantDAO.selectByRoleTypeAndRoleStatus(RoleMerchant.ROLE_TYPE_MERCHANT_USER_ROLE,
		RoleMerchant.STATUS_ACTIVE);
	if (roleMerchants != null && !roleMerchants.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (RoleMerchant roleMerchant : roleMerchants) {
		entryBeans.add(new EntryBean(roleMerchant.getId(), roleMerchant.getRoleName()));
	    }
	}
	entryBeans = this.sortListLongByValue(entryBeans);
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectMerchantUserStatusByCurrentStatus(String status) {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	String prefix = "com.fw.service.masterdata.merchantUserStatus.";
	String key = null;
	String value = null;
	key = prefix + status;
	value = masterDataProperties.getProperty(key, null);
	entryBeans.add(new EntryBean<String, String>(status, value));
	if (MerchantUser.STATUS_ACTIVE.equals(status)) {
	    key = prefix + MerchantUser.STATUS_EXPIRED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantUser.STATUS_EXPIRED, value));
	    key = prefix + MerchantUser.STATUS_LOCKED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantUser.STATUS_LOCKED, value));
	    key = prefix + MerchantUser.STATUS_INACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantUser.STATUS_INACTIVE, value));
	} else if (MerchantUser.STATUS_EXPIRED.equals(status) || MerchantUser.STATUS_LOCKED.equals(status)) {
	    key = prefix + MerchantUser.STATUS_ACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantUser.STATUS_ACTIVE, value));
	} else if (MerchantUser.STATUS_INACTIVE.equals(status)) {
	    key = prefix + MerchantUser.STATUS_ACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantUser.STATUS_ACTIVE, value));
	    key = prefix + MerchantUser.STATUS_CLOSED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(MerchantUser.STATUS_CLOSED, value));
	} else if (MerchantUser.STATUS_CLOSED.equals(status)) {
	    // value = masterDataProperties.getProperty(prefix + BOUser.STATUS_INACTIVE, null);
	    // entryBeans.add(new EntryBean<String, String>(value, value));
	}
	entryBeans = this.sortListStringByValue(entryBeans);
	return entryBeans;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List<EntryBean<Long, String>> selectAllMerchantUserMerchantProfile() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<MerchantProfile> merchantProfiles = merchantProfilePersistenceService.selectByNotStatus(MerchantProfile.STATUS_CLOSED);
	if (merchantProfiles != null && !merchantProfiles.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (MerchantProfile merchantProfile : merchantProfiles) {
		entryBeans.add(new EntryBean(merchantProfile.getId(), merchantProfile.getMerchantId()));
	    }
	}
	return entryBeans;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List<EntryBean<Long, String>> selectAllMerchantUserParentMerchant() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<ParentMerchant> paranetMerchants = parentMerchantDAO.selectByNotParentStatus(ParentMerchant.STATUS_INACTIVE);
	if (paranetMerchants != null && !paranetMerchants.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (ParentMerchant parentMerchant : paranetMerchants) {
		entryBeans.add(new EntryBean(parentMerchant.getId(), parentMerchant.getParentName()));
	    }
	}
	return entryBeans;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List<EntryBean<Long, String>> selectAllMerchantUserUsername() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<MerchantUser> merchantUsers = merchantUserPersistenceService.selectByStatus(null);
	if (merchantUsers != null && !merchantUsers.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (MerchantUser merchantUser : merchantUsers) {
		entryBeans.add(new EntryBean(merchantUser.getId(), merchantUser.getUsername()));
	    }
	}
	entryBeans = this.sortListLongByValue(entryBeans);
	return entryBeans;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List<EntryBean<Long, String>> selectAllMerchantUserName() {
	List<EntryBean<Long, String>> entryBeans = null;
	List<MerchantUser> merchantUsers = merchantUserPersistenceService.selectByStatus(null);
	if (merchantUsers != null && !merchantUsers.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<Long, String>>();
	    for (MerchantUser merchantUser : merchantUsers) {
		if (merchantUser.getName() != null) {
		    entryBeans.add(new EntryBean(merchantUser.getId(), merchantUser.getName()));
		}
	    }
	}
	entryBeans = this.sortListLongByValue(entryBeans);
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantUserCreateBy() {
	List<EntryBean<String, String>> entryBeans = null;
	List<Object> objects = merchantUserPersistenceService.selectDistinctCreateBy();
	if (objects != null && !objects.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<String, String>>();
	    for (Object object : objects) {
		String createBy = (String) object;
		entryBeans.add(new EntryBean<String, String>(createBy, createBy));
	    }
	    entryBeans = this.sortListStringByValue(entryBeans);
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantUserLastUpdateBy() {
	List<EntryBean<String, String>> entryBeans = null;
	List<Object> objects = merchantUserPersistenceService.selectDistinctLastUpdateBy();
	if (objects != null && !objects.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<String, String>>();
	    for (Object object : objects) {
		String lastUpdateBy = (String) object;
		entryBeans.add(new EntryBean<String, String>(lastUpdateBy, lastUpdateBy));
	    }
	    entryBeans = this.sortListStringByValue(entryBeans);
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectAllMerchantUserEmail() {
	List<EntryBean<String, String>> entryBeans = null;
	List<Object> objects = merchantUserPersistenceService.selectDistinctEmail();
	if (objects != null && !objects.isEmpty()) {
	    entryBeans = new ArrayList<EntryBean<String, String>>();
	    for (Object object : objects) {
		String email = (String) object;
		if (email != null && !email.isEmpty()) {
		    entryBeans.add(new EntryBean<String, String>(email, email));
		}
	    }
	    entryBeans = this.sortListStringByValue(entryBeans);
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<Long, String>> selectAllMerchantUserServiceProfile(String merchantID) {
	List<EntryBean<Long, String>> entryBeans = new ArrayList<EntryBean<Long, String>>();
	List<ServiceProfile> serviceProfiles = serviceProfilePersistenceService.selectByMerchantIdAndNotStatus(merchantID,
		ServiceProfile.STATUS_CLOSED);
	if (serviceProfiles != null && !serviceProfiles.isEmpty()) {
	    for (ServiceProfile serviceProfile : serviceProfiles) {
		entryBeans.add(new EntryBean<Long, String>(serviceProfile.getId(), serviceProfile.getServiceId()));
	    }
	}
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllServiceProfileServiceId() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> items = serviceProfilePersistenceService.selectDistinctServiceId();
	if (items != null && !items.isEmpty()) {
	    for (Object item : items) {
		String data = (String) item;
		if (data == null || data.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(data, data));
	    }
	}
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllServiceProfileMerchantId() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> items = serviceProfilePersistenceService.selectDistinctMerchantId();
	if (items != null && !items.isEmpty()) {
	    for (Object item : items) {
		String data = (String) item;
		if (data == null || data.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(data, data));
	    }
	}
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllServiceProfileMerchantName() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> items = serviceProfilePersistenceService.selectDistinctMerchantName();
	if (items != null && !items.isEmpty()) {
	    for (Object item : items) {
		String data = (String) item;
		if (data == null || data.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(data, data));
	    }
	}
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllServiceProfileCompanyName() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> items = serviceProfilePersistenceService.selectDistinctCompanyName();
	if (items != null && !items.isEmpty()) {
	    for (Object item : items) {
		String data = (String) item;
		if (data == null || data.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(data, data));
	    }
	}
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllServiceProfileProjectContact() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> items = serviceProfilePersistenceService.selectDistinctContact1Name();
	if (items != null && !items.isEmpty()) {
	    for (Object item : items) {
		String data = (String) item;
		if (data == null || data.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(data, data));
	    }
	}
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllServiceProfileMdr() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> items = serviceProfilePersistenceService.selectDistinctMdr();
	if (items != null && !items.isEmpty()) {
	    for (Object item : items) {
		String data = (String) item;
		if (data == null || data.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(data, data));
	    }
	}
	return entryBeans;
    }

    public List<EntryBean<String, String>> selectAllServiceProfileServiceFee() {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	List<Object> items = serviceProfilePersistenceService.selectDistinctServiceFee();
	if (items != null && !items.isEmpty()) {
	    for (Object item : items) {
		String data = (String) item;
		if (data == null || data.isEmpty()) {
		    continue;
		}
		entryBeans.add(new EntryBean<String, String>(data, data));
	    }
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectServiceProfileStatusByCurrentStatus(String status) {
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	String prefix = "com.fw.service.masterdata.serviceProfileStatus.";
	String key = null;
	String value = null;
	key = prefix + status;
	value = masterDataProperties.getProperty(key, null);
	entryBeans.add(new EntryBean<String, String>(status, value));
	if (ServiceProfile.STATUS_ACTIVE.equals(status)) {
	    key = prefix + ServiceProfile.STATUS_SUSPENED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(ServiceProfile.STATUS_SUSPENED, value));
	    key = prefix + ServiceProfile.STATUS_INACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(ServiceProfile.STATUS_INACTIVE, value));
	} else if (ServiceProfile.STATUS_SUSPENED.equals(status)) {
	    key = prefix + ServiceProfile.STATUS_ACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(ServiceProfile.STATUS_ACTIVE, value));
	    key = prefix + ServiceProfile.STATUS_INACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(ServiceProfile.STATUS_INACTIVE, value));
	} else if (ServiceProfile.STATUS_INACTIVE.equals(status)) {
	    key = prefix + ServiceProfile.STATUS_ACTIVE;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(ServiceProfile.STATUS_ACTIVE, value));
	    key = prefix + ServiceProfile.STATUS_SUSPENED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(ServiceProfile.STATUS_SUSPENED, value));
	    key = prefix + ServiceProfile.STATUS_CLOSED;
	    value = masterDataProperties.getProperty(key, null);
	    entryBeans.add(new EntryBean<String, String>(ServiceProfile.STATUS_CLOSED, value));
	} else if (ServiceProfile.STATUS_CLOSED.equals(status)) {
	}
	entryBeans = this.sortListStringByValue(entryBeans);
	return entryBeans;
    }
}
